package com.fare.taluo.core.domain.analyse.alert;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Where;

import com.dayatang.domain.InstanceFactory;
import com.fare.taluo.GlobalsConst;
import com.fare.taluo.auth.domain.UserInfoEntity;
import com.fare.taluo.core.domain.queue.warn.QueueFreightPriceWarnEntity;
import com.fare.taluo.core.domain.queue.warn.repository.QueueFreightPriceWarnReposity;
/**
 * 运价分析预警配置
 * @author creator: xiangsf
 * @author modifior: xiangsf
 * @since 2012-12-24-上午11:25:18
 * @version v1.0
 */
@javax.persistence.Entity
@Table(name="FDC_ALERT_CONFIG")
@Where(clause="flg_deleted=0")
public class FreightPriceAnalyseAlertConfigEntity extends com.fare.taluo.base.ddd.domain.baseentity.BaseEntity{
	/**
	 * 预警方式，1邮件
	 */
	public static final int CONFIG_ALERT_WAY_EMAIL = 1;
	/**
	 * 预警方式，2短信
	 */
	public static final int CONFIG_ALERT_WAY_SMS = 2;
	/**
	 * 航班类型，1单程
	 */
	public static final int CONFIG_BOOKING_TYPE_SINGLE = 1;
	/**
	 * 航班类型，2来回程
	 */
	public static final int CONFIG_BOOKING_TYPE_DOUBLE = 2;
	/**
	 * 
	 */
	private static final long serialVersionUID = 375171077416722709L;

	/**
	 * 主键ID
	 */
//	@Id
//	@GeneratedValue(strategy = GenerationType.AUTO, generator = FDC_SEQ_GEN)
//	@SequenceGenerator(name = FDC_SEQ_GEN, sequenceName = FDC_SEQUENCE)
//	private Long id;
	/**
	 * 创建人ID
	 */
	@Column(name = "creator")
	private Long creator;
	/**
	 * 创建人名称
	 */
	@Column(name = "creator_name")
	private String creatorName;
	/**
	 * 创建时间,默认当前时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "create_time")
	private Date createTime = new Date();
	/**
	 * 更新人ID
	 */
	@Column(name = "updator")
	private Long updator;
	/**
	 * 更新人名称
	 */
	@Column(name = "updator_name")
	private String updatorName;
	/**
	 * 最后更新时间,默认当前时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "update_time")
	private Date updateTime = new Date();
	/**
	 * 逻辑删除标识
	 */
	@Column(name = "FLG_DELETED")	
	protected int flgDeleted = GlobalsConst.FLG_NO;
	/**
	 * 出发城市代码
	 */
	@Column(name = "FROM_CITY")
	private String fromCity;
	/**
	 * 到达城市代码
	 */
	@Column(name = "TO_CITY")
	private String toCity;
	/**
	 * 出发城市名称
	 */
	@Column(name = "FROM_CITY_NAME")
	private String fromCityName;
	/**
	 * 到达城市名称
	 */
	@Column(name = "TO_CITY_NAME")
	private String toCityName;
	/**
	 * 预警开始时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "start_time")
	private Date startTime;
	/**
	 * 预警结束时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "end_time")
	private Date endTime;
	/**
	 * 差价比,正数为高，负数为低，取百分比值
	 */
	@Column(name = "spread_than")
	private double spreadThan = 0L;
	/**
	 * 预警方式，分1、邮件、2、短信，3、可多选
	 * @see FreightPriceAnalyseAlertConfig.CONFIG_ALERT_WAY_EMAIL
	 * @see FreightPriceAnalyseAlertConfig.CONFIG_ALERT_WAY_SMS
	 */
	@Column(name = "alert_way")
	private int alertWay = 1;
	/**
	 * 未来天数
	 */
	@Column(name = "future_days")
	private Integer futureDays;
	/**
	 *触发条件
	 */
	@Column(name = "trigger_condition")
	private Integer triggerCondition;
	/**
	 * 航空公司名
	 */
	@Column(name = "airCompany")
	private String airCompany;
	/**
	 * 预警频率（单位：小时）,默认半天(12小时)
	 */
	@Column(name = "alert_frequency")
	private int alertFrequency = 12;
	
	@Column(name= "area")
	private String area;
	
	@Column(name = "area_code")
	private String areaCode;
	
	public int getFlgDeleted() {
		return flgDeleted;
	}
	public void setFlgDeleted(int flgDeleted) {
		this.flgDeleted = flgDeleted;
	}
	
	

	/**
	 * 订票类型
	 * <p>值参考：CONFIG_BOOKING_TYPE_SINGLE,CONFIG_BOOKING_TYPE_DOUBLE
	 */
	@Column(name = "booking_Type")
	private int bookingType = CONFIG_BOOKING_TYPE_SINGLE;
	
	/**
	 * 预警类型
	 */
	@Column(name = "alert_type")
	private Integer alertType ;
	
	/**
	 * 航空公司的carrierkey
	 */
	@Column(name="carrier_key")
	private String carrierKey;
	
	/**
	 * 航班起飞日期
	 */
	@Column(name="flightdate")
	private Date flightDate;
	
	/**
	 * 舱位等级
	 */
	@Column(name="cabin_level")
	private String cabinLevel;
	
	/**
	 * 对比渠道
	 */
	@Column(name="contrast_source")
	private String contrastSource;
	
	/**
	 * 对比渠道的key 两位
	 */
	@Column(name="contrast_source_key")
	private String contrastSourceKey;
	
	/**
	 * 对比航空公司
	 */
	@Column(name="contrast_aircompany")
	private String contrastAircompany;
	/**
	 * 对比航空公司的carrierkey 两位
	 */
	@Column(name="contrast_aircompany_key")
	private String contrastAircompanyKey;
	/**
	 * 对比航班号
	 */
	@Column(name="contrast_flightno")
	private String contrastFlightNo;
	
	@Column(name="SPREAD_RADIO")
	private Integer triggerConditionSpread;
	
	@Column(name="FLIGHTNO")
	private String flightNo;
	
	/**
	 * 对比舱位等级
	 */
	@Column(name="contrast_cabin_level")
	private String contrastCabinLevel;
	
//	public Long getId() {
//		return id;
//	}
//	public void setId(Long id) {
//		this.id = id;
//	}
	public String getFromCity() {
		return fromCity;
	}
	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}
	public String getToCity() {
		return toCity;
	}
	public void setToCity(String toCity) {
		this.toCity = toCity;
	}
	public String getFromCityName() {
		return fromCityName;
	}
	public void setFromCityName(String fromCityName) {
		this.fromCityName = fromCityName;
	}
	public String getToCityName() {
		return toCityName;
	}
	public void setToCityName(String toCityName) {
		this.toCityName = toCityName;
	}
	
	public double getSpreadThan() {
		return spreadThan;
	}
	public void setSpreadThan(double spreadThan) {
		this.spreadThan = spreadThan;
	}
	public int getAlertWay() {
		return alertWay;
	}
	public void setAlertWay(int alertWay) {
		this.alertWay = alertWay;
	}
	public int getAlertFrequency() {
		//update xiangsf 2013-09-28 这里将频率写死一个，仓位变化的频率必须是一小时一次，要与预处理一致。
		if(this.getAlertType().intValue() == GlobalsConst.ALERT_TYPE_CABIN_CHANGE) this.alertFrequency = 1;
		return alertFrequency;
	}
	public void setAlertFrequency(int alertFrequency) {
		this.alertFrequency = alertFrequency;
	}
	public Long getCreator() {
		return creator;
	}
	public void setCreator(Long creator) {
		this.creator = creator;
	}
	public String getCreatorName() {
		return creatorName;
	}
	public void setCreatorName(String creatorName) {
		this.creatorName = creatorName;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Long getUpdator() {
		return updator;
	}
	public void setUpdator(Long updator) {
		this.updator = updator;
	}
	public String getUpdatorName() {
		return updatorName;
	}
	public void setUpdatorName(String updatorName) {
		this.updatorName = updatorName;
	}
	public Date getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	public Date getStartTime() {
		return startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	public int getBookingType() {
		return bookingType;
	}
	public void setBookingType(int bookingType) {
		this.bookingType = bookingType;
	}
	public boolean isEmail(){
		return this.alertWay == 1 || this.alertWay ==3;
	}
	
	public boolean isSms(){
		return this.alertWay == 2 || this.alertWay ==3;
	}
	
		
	
	
	
	public Integer getAlertType() {
		return alertType;
	}
	public void setAlertType(Integer alertType) {
		this.alertType = alertType;
	}
	
	public String getCabinLevel() {
		return cabinLevel;
	}
	public void setCabinLevel(String cabinLevel) {
		this.cabinLevel = cabinLevel;
	}
	public String getCarrierKey() {
		return carrierKey;
	}
	public void setCarrierKey(String carrierKey) {
		this.carrierKey = carrierKey;
	}
	public Date getFlightDate() {
		return flightDate;
	}
	public void setFlightDate(Date flightDate) {
		this.flightDate = flightDate;
	}
	
	public String getContrastSource() {
		return contrastSource;
	}
	public void setContrastSource(String contrastSource) {
		this.contrastSource = contrastSource;
	}
	public String getContrastSourceKey() {
		return contrastSourceKey;
	}
	public void setContrastSourceKey(String contrastSourceKey) {
		this.contrastSourceKey = contrastSourceKey;
	}
	public String getContrastAircompany() {
		return contrastAircompany;
	}
	public void setContrastAircompany(String contrastAircompany) {
		this.contrastAircompany = contrastAircompany;
	}
	public String getContrastAircompanyKey() {
		return contrastAircompanyKey;
	}
	public void setContrastAircompanyKey(String contrastAircompanyKey) {
		this.contrastAircompanyKey = contrastAircompanyKey;
	}
	public String getContrastFlightNo() {
		return contrastFlightNo;
	}
	public void setContrastFlightNo(String contrastFlightNo) {
		this.contrastFlightNo = contrastFlightNo;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + alertFrequency;
		result = prime * result + alertWay;
		result = prime * result + bookingType;
		result = prime * result
				+ ((createTime == null) ? 0 : createTime.hashCode());
		result = prime * result + ((creator == null) ? 0 : creator.hashCode());
		result = prime * result
				+ ((creatorName == null) ? 0 : creatorName.hashCode());
		result = prime * result + ((endTime == null) ? 0 : endTime.hashCode());
		result = prime * result
				+ ((fromCity == null) ? 0 : fromCity.hashCode());
		result = prime * result
				+ ((fromCityName == null) ? 0 : fromCityName.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		long temp;
		temp = Double.doubleToLongBits(spreadThan);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result
				+ ((startTime == null) ? 0 : startTime.hashCode());
		result = prime * result + ((toCity == null) ? 0 : toCity.hashCode());
		result = prime * result
				+ ((toCityName == null) ? 0 : toCityName.hashCode());
		result = prime * result
				+ ((updateTime == null) ? 0 : updateTime.hashCode());
		result = prime * result + ((updator == null) ? 0 : updator.hashCode());
		result = prime * result
				+ ((updatorName == null) ? 0 : updatorName.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof FreightPriceAnalyseAlertConfigEntity))
			return false;
		FreightPriceAnalyseAlertConfigEntity other = (FreightPriceAnalyseAlertConfigEntity) obj;
		if (alertFrequency != other.alertFrequency)
			return false;
		if (alertWay != other.alertWay)
			return false;
		if (bookingType != other.bookingType)
			return false;
		if (createTime == null) {
			if (other.createTime != null)
				return false;
		} else if (!createTime.equals(other.createTime))
			return false;
		if (creator == null) {
			if (other.creator != null)
				return false;
		} else if (!creator.equals(other.creator))
			return false;
		if (creatorName == null) {
			if (other.creatorName != null)
				return false;
		} else if (!creatorName.equals(other.creatorName))
			return false;
		if (endTime == null) {
			if (other.endTime != null)
				return false;
		} else if (!endTime.equals(other.endTime))
			return false;
		if (fromCity == null) {
			if (other.fromCity != null)
				return false;
		} else if (!fromCity.equals(other.fromCity))
			return false;
		if (fromCityName == null) {
			if (other.fromCityName != null)
				return false;
		} else if (!fromCityName.equals(other.fromCityName))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (Double.doubleToLongBits(spreadThan) != Double
				.doubleToLongBits(other.spreadThan))
			return false;
		if (startTime == null) {
			if (other.startTime != null)
				return false;
		} else if (!startTime.equals(other.startTime))
			return false;
		if (toCity == null) {
			if (other.toCity != null)
				return false;
		} else if (!toCity.equals(other.toCity))
			return false;
		if (toCityName == null) {
			if (other.toCityName != null)
				return false;
		} else if (!toCityName.equals(other.toCityName))
			return false;
		if (updateTime == null) {
			if (other.updateTime != null)
				return false;
		} else if (!updateTime.equals(other.updateTime))
			return false;
		if (updator == null) {
			if (other.updator != null)
				return false;
		} else if (!updator.equals(other.updator))
			return false;
		if (updatorName == null) {
			if (other.updatorName != null)
				return false;
		} else if (!updatorName.equals(other.updatorName))
			return false;
		return true;
	}
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("FreightPriceAnalyseAlertConfig [id=");
		builder.append(id);
		builder.append(", creator=");
		builder.append(creator);
		builder.append(", creatorName=");
		builder.append(creatorName);
		builder.append(", createTime=");
		builder.append(createTime);
		builder.append(", updator=");
		builder.append(updator);
		builder.append(", updatorName=");
		builder.append(updatorName);
		builder.append(", updateTime=");
		builder.append(updateTime);
		builder.append(", fromCity=");
		builder.append(fromCity);
		builder.append(", toCity=");
		builder.append(toCity);
		builder.append(", fromCityName=");
		builder.append(fromCityName);
		builder.append(", toCityName=");
		builder.append(toCityName);
		builder.append(", startTime=");
		builder.append(startTime);
		builder.append(", endTime=");
		builder.append(endTime);
		builder.append(", spreadThan=");
		builder.append(spreadThan);
		builder.append(", alertWay=");
		builder.append(alertWay);
		builder.append(", alertFrequency=");
		builder.append(alertFrequency);
		builder.append(", bookingType=");
		builder.append(bookingType);
		builder.append("]");
		return builder.toString();
	}
	

	public FreightPriceAnalyseAlertConfigRepository getEntityRepository(){
		return InstanceFactory.getInstance(FreightPriceAnalyseAlertConfigRepository.class);
	}
	
	public QueueFreightPriceWarnReposity getQueueFreightPriceWarnReposity(){
		return InstanceFactory.getInstance(QueueFreightPriceWarnReposity.class);
	}
	
	//查询所有
	public List<FreightPriceAnalyseAlertConfigEntity> getAllAnalyseAlert(Long userID){
		return getEntityRepository().getAllAnalyseAlert(userID);
	}

	//保存
	@Override
	public void save(){
		getEntityRepository().save(this);
		//产生预警任务队列
		QueueFreightPriceWarnEntity queueEntity = new QueueFreightPriceWarnEntity();
		queueEntity.setDelegateId(this.getId());
		queueEntity.setName("运价预警产生队列任务");
		queueEntity.setPlanStartTime(this.getStartTime()==null?new Date():this.getStartTime());
		queueEntity.setEndTime(this.getFlightDate()==null?new Date():this.getFlightDate());
		queueEntity.setCreator(this.getCreator());
		queueEntity.setCreatorName(this.getCreatorName());
		UserInfoEntity user = getEntityRepository().get(UserInfoEntity.class, this.getCreator());
		queueEntity.setEmailAddress(user.getEmail());
		queueEntity.setSmsAddress(user.getMobile());
		getEntityRepository().save(queueEntity);
	}

	//组合条件查询
	public List<FreightPriceAnalyseAlertConfigEntity> getAllAnalyseAlertByC(String alertType ,Date createTime,Date endCreateTime,Long userId){
		return getEntityRepository().getAllAnalyseAlertByC(alertType , createTime,endCreateTime,userId);
	}

	/**
	 * 查询航空公司下指定航线的预警配置
	 * @param airCompany
	 * @param fromCityCode
	 * @param toCityCode
	 * @return
	 */
	public FreightPriceAnalyseAlertConfigEntity getConfig(String hbaseKey, String fromCityCode, String toCityCode, Long userId){
		return getEntityRepository().getConfigByLine(hbaseKey, fromCityCode, toCityCode, userId);
	}
	
	public FreightPriceAnalyseAlertConfigEntity getCabinChangeConfig(String flightNo,String flightDate,Long userId){
		return getEntityRepository().getCabinChangeConfig( flightNo, flightDate, userId);
	}
	public FreightPriceAnalyseAlertConfigEntity getSpreadConfig(String flightNo,String flightDate,String contrastSourceKey,Long userId){
		return getEntityRepository().getSpreadConfig( flightNo, flightDate, contrastSourceKey,userId);
	}
	
	public FreightPriceAnalyseAlertConfigEntity getContrastConfig(String flightNo,String contrastFlightNo,String flightDate,Long userId){
		return getEntityRepository().getContrastConfig( flightNo,contrastFlightNo, flightDate,userId);
	}
	
	//根据主键查询
	public FreightPriceAnalyseAlertConfigEntity getFreightPriceAnalyseAlertConfig(Long id){
		return getEntityRepository().get(FreightPriceAnalyseAlertConfigEntity.class, id);
	}

	public void delete(){
		this.setFlgDeleted(GlobalsConst.FLG_YES);
		getEntityRepository().save(this);
		getQueueFreightPriceWarnReposity().deleteWait(this.getId());
		
	}
	public int getFutureDays() {
		return futureDays;
	}
	public void setFutureDays(int futureDays) {
		this.futureDays = futureDays;
	}
	public int getTriggerCondition() {
		return triggerCondition;
	}
	public void setTriggerCondition(int triggerCondition) {
		this.triggerCondition = triggerCondition;
	}
	public String getAirCompany() {
		return airCompany;
	}
	public void setAirCompany(String airCompany) {
		this.airCompany = airCompany;
	}
	public Integer getTriggerConditionSpread() {
		return triggerConditionSpread;
	}
	public void setTriggerConditionSpread(Integer triggerConditionSpread) {
		this.triggerConditionSpread = triggerConditionSpread;
	}
	public void setFutureDays(Integer futureDays) {
		this.futureDays = futureDays;
	}
	public void setTriggerCondition(Integer triggerCondition) {
		this.triggerCondition = triggerCondition;
	}
	public String getFlightNo() {
		return flightNo;
	}
	public void setFlightNo(String flightNo) {
		this.flightNo = flightNo;
	}
	public String getContrastCabinLevel() {
		return contrastCabinLevel;
	}
	public void setContrastCabinLevel(String contrastCabinLevel) {
		this.contrastCabinLevel = contrastCabinLevel;
	}
	public String getArea() {
		return area;
	}
	public void setArea(String area) {
		this.area = area;
	}
	public String getAreaCode() {
		return areaCode;
	}
	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}
}
